

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午7:42:59
* All right reserved.
*
*/
	
package com.qf.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qf.dto.CrmCourseDto;
import com.qf.dto.CrmRunoffDto;
import com.qf.dto.CrmStudentDto;
import com.qf.pojo.CrmClass;
import com.qf.pojo.CrmRunoff;
import com.qf.pojo.CrmStaff;
import com.qf.pojo.CrmStation;
import com.qf.pojo.CrmStudent;
import com.qf.pojo.PageRefer;
import com.qf.pojo.PageRunoff;
import com.qf.pojo.PageStation;
import com.qf.pojo.PageStudent;
import com.qf.service.CrmClassService;
import com.qf.service.CrmCourseTypeService;
import com.qf.service.CrmStudentService;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月27日 下午7:42:59
* @history:
* @version: v1.0

*/
@Controller
public class CrmStudentServlet {
    @Autowired
    @Qualifier("crmStudentService")
	private CrmStudentService studentService;
    @Autowired
    @Qualifier("crmCourseTypeService")
	private CrmCourseTypeService cts;
    @Autowired
    @Qualifier("crmClassService")
    private CrmClassService classservice;

	
	/**
	* @param studentService the studentService to set
	*/
	
	public void setStudentService(CrmStudentService studentService) {
	
		this.studentService = studentService;
	}


	
	/**
	* @param cts the cts to set
	*/
	
	public void setCts(CrmCourseTypeService cts) {
	
		this.cts = cts;
	}
	
	
	/**
	* @param classservice the classservice to set
	*/
	
	public void setClassservice(CrmClassService classservice) {
	
		this.classservice = classservice;
	}



	@RequestMapping("/allcourses.do")
	public ModelAndView allcourses(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		List<CrmCourseDto> allCourse = cts.findbyAllCourse();
		mv.addObject("allCourse", allCourse);
		mv.setViewName("/pages/student/addOrEditStudent.jsp");
		return mv;	
		
	}
	@RequestMapping("/addCrmStudent.do")
	public ModelAndView addCrmStudent(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String stuName=request.getParameter("stuName");
		String gender=request.getParameter("gender");
		String mustTuition=request.getParameter("mustTuition");
		String actualTuition=request.getParameter("actualTuition");
		String identity=request.getParameter("identity");
		String telephone=request.getParameter("telephone");
		String qq=request.getParameter("qq");
		String email=request.getParameter("email");
		String classId=request.getParameter("classId");
		String school=request.getParameter("school");
		String education=request.getParameter("education");
		String professional=request.getParameter("professional");
		String identityAddress=request.getParameter("identityAddress");
		String address=request.getParameter("address");
		String remark=request.getParameter("remark");
		String homeTelephone=request.getParameter("homeTelephone");
		String homeContact=request.getParameter("homeContact");
		CrmClass crmClass = classservice.findClassbyId(Long.valueOf(classId));
		CrmStudent crmStudent = new CrmStudent();
		crmStudent.setStuName(stuName);
		crmStudent.setGender(gender);
		crmStudent.setMustTuition(Integer.parseInt(mustTuition));
		crmStudent.setActualTuition(Integer.parseInt(actualTuition));
		crmStudent.setIdentity(identity);
		crmStudent.setTelephone(telephone);
		crmStudent.setQq(qq);
		crmStudent.setEmail(email);
		crmStudent.setCrmClass(crmClass);
		crmStudent.setSchool(school);
		crmStudent.setEducation(education);
		crmStudent.setProfessional(professional);
		crmStudent.setIdentityAddress(identityAddress);
		crmStudent.setAddress(address);
		crmStudent.setRemark(remark);
		crmStudent.setHomeTelephone(homeTelephone);
		crmStudent.setHomeContact(homeContact);
		crmStudent.setStatus("新生");
		studentService.addCrmStudent(crmStudent);
		//更新学生总数
		int totalCount = studentService.findStudentCountbyother(Long.valueOf(classId));
		crmClass.setTotalCount(totalCount);
		classservice.updateClass(crmClass);	
		
		mv.setViewName("findCrmStudentByPage.do");
		return mv;
		
		
    
}
	@RequestMapping("/findCrmStudentByPage.do")
	public ModelAndView findCrmStudentByPage(HttpServletRequest request,HttpServletResponse response){
		    ModelAndView mv=new ModelAndView();
	    	String page_=request.getParameter("page");
	    	int page=1;
	    	int size=3;
	    	if(page_!=null){
	    		page=Integer.parseInt(page_);
	    	}
	    	PageStudent pageStudent = studentService.findStudentByPage(page, size);
	    	mv.addObject("pageStudent", pageStudent);
	    	mv.setViewName("/pages/student/listStudent.jsp");
	    	return mv;
	
}
	@RequestMapping("/preupdateCrmStudent.do")
	public ModelAndView preupdateCrmStudent(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId=request.getParameter("studentId");
		CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
		mv.addObject("studentDto", studentDto);
		List<CrmCourseDto> allCourse = cts.findbyAllCourse();
		mv.addObject("allCourse", allCourse);
		mv.setViewName("/pages/student/editStudent.jsp");	
		return mv;	
	
}
	@RequestMapping("/updateCrmStudent.do")
	public ModelAndView updateCrmStudent(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId=request.getParameter("studentId");
		String oldclassId=request.getParameter("oldclassId");
		String stuName=request.getParameter("stuName");
		String gender=request.getParameter("gender");
		String mustTuition=request.getParameter("mustTuition");
		String actualTuition=request.getParameter("actualTuition");
		String identity=request.getParameter("identity");
		String telephone=request.getParameter("telephone");
		String qq=request.getParameter("qq");
		String email=request.getParameter("email");
		String newclassId=request.getParameter("newclassId");
		String school=request.getParameter("school");
		String education=request.getParameter("education");
		String professional=request.getParameter("professional");
		String identityAddress=request.getParameter("identityAddress");
		String address=request.getParameter("address");
		String remark=request.getParameter("remark");
		String homeTelephone=request.getParameter("homeTelephone");
		String homeContact=request.getParameter("homeContact");
		CrmClass crmClass = classservice.findClassbyId(Long.valueOf(newclassId));
		CrmStudent crmStudent = studentService.findCrmStudentbyId(Long.valueOf(studentId));
		crmStudent.setStuName(stuName);
		crmStudent.setGender(gender);
		crmStudent.setMustTuition(Integer.parseInt(mustTuition));
		crmStudent.setActualTuition(Integer.parseInt(actualTuition));
		crmStudent.setIdentity(identity);
		crmStudent.setTelephone(telephone);
		crmStudent.setQq(qq);
		crmStudent.setEmail(email);
		crmStudent.setCrmClass(crmClass);
		crmStudent.setSchool(school);
		crmStudent.setEducation(education);
		crmStudent.setProfessional(professional);
		crmStudent.setIdentityAddress(identityAddress);
		crmStudent.setAddress(address);
		crmStudent.setRemark(remark);
		crmStudent.setHomeTelephone(homeTelephone);
		crmStudent.setHomeContact(homeContact);
		crmStudent.setStatus("新生");
		studentService.updateCrmStudent(crmStudent);
		//更新学生总数
		if(!oldclassId.equals(newclassId)){
		int newtotalCount= studentService.findStudentCountbyother(Long.valueOf(newclassId));
		crmClass.setTotalCount(newtotalCount);
		classservice.updateClass(crmClass);	
		CrmClass oldcrmClass = classservice.findClassbyId(Long.valueOf(oldclassId));
		int oldtotalCount= studentService.findStudentCountbyother(Long.valueOf(oldclassId));
		oldcrmClass.setTotalCount(oldtotalCount);
		classservice.updateClass(oldcrmClass);	
		mv.setViewName("findCrmStudentByPage.do");
		}else{
			mv.setViewName("findCrmStudentByPage.do");
		}
		return mv;
		
	}
	@RequestMapping("/preaddStation.do")
	public ModelAndView preaddStation(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId = request.getParameter("studentId");
		CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
		if("退学".equals(studentDto.getStatus())||"毕业".equals(studentDto.getStatus())){
			mv.setViewName("/pages/station/tiaozhuan.jsp");
		}else{
		mv.addObject("studentDto", studentDto);
		List<CrmCourseDto> allCourse = cts.findbyAllCourse();
		mv.addObject("allCourse", allCourse);
		mv.setViewName("/pages/station/addStation.jsp");
		}
		return mv;
}
	
	/**
	* @author: 千锋周锐
	* @createTime: 2017年7月29日 下午10:18:00
	* @history:
	* @param request
	* @param response
	* @return ModelAndView
	*/
		
	@RequestMapping("/addStation.do")
	public ModelAndView addStation(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId=request.getParameter("studentId");
		String flag=request.getParameter("flag");
		if("毕业".equals(flag)){
			CrmStudent crmStudent = studentService.findCrmStudentbyId(Long.valueOf(studentId));
			crmStudent.setStatus(flag);
			studentService.updateCrmStudent(crmStudent);
			//更新学生总数
			CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
			Long classId=studentDto.getClassId();
			CrmClass crmClass = classservice.findClassbyId(Long.valueOf(classId));
			int totalCount = studentService.findStudentCountbyother(Long.valueOf(classId));
			crmClass.setTotalCount(totalCount);
			classservice.updateClass(crmClass);
			mv.setViewName("findCrmStudentByPage.do");
		}else if("退学".equals(flag)){
			mv.addObject("studentId", studentId);
			mv.setViewName("preaddRunoff.do");
		}else if("转班".equals(flag)){
			CrmStudent crmStudent = studentService.findCrmStudentbyId(Long.valueOf(studentId));
			crmStudent.setStatus(flag);
			studentService.updateCrmStudent(crmStudent);
			String afterClassId=request.getParameter("afterClassId");
			CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
			Long beforeClassId=studentDto.getClassId();
			CrmClass crmClass = classservice.findClassbyId(Long.valueOf(afterClassId));
			crmStudent.setCrmClass(crmClass);
			studentService.updateCrmStudent(crmStudent);
			CrmStaff crmStaff=(CrmStaff)request.getSession().getAttribute("crmStaff");
			CrmStation crmStation=new CrmStation(null, crmStudent, flag, crmStaff.getStaffId(), System.currentTimeMillis(), beforeClassId, Long.valueOf(afterClassId));
			studentService.addStation(crmStation);
			//更新升级
			int upgradeCount = studentService.findAdvanceCountbyother(Long.valueOf(afterClassId));
			int totalCount=studentService.findStudentCountbyother(Long.valueOf(afterClassId));
			crmClass.setUpgradeCount(upgradeCount);
			crmClass.setTotalCount(totalCount);
			classservice.updateClass(crmClass);
			//更新学生总数
			CrmClass crmClass2 = classservice.findClassbyId(Long.valueOf(beforeClassId));
			int totalCount2 = studentService.findStudentCountbyother(Long.valueOf(beforeClassId));
			crmClass2.setTotalCount(totalCount2);
			classservice.updateClass(crmClass2);
			mv.setViewName("/findStationByPage.do");
		}else if("升级".equals(flag)){
			CrmStudent crmStudent = studentService.findCrmStudentbyId(Long.valueOf(studentId));
			crmStudent.setStatus(flag);
			studentService.updateCrmStudent(crmStudent);
			String afterClassId=request.getParameter("afterClassId");
			CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
			Long beforeClassId=studentDto.getClassId();
			CrmClass crmClass = classservice.findClassbyId(Long.valueOf(afterClassId));
			crmStudent.setCrmClass(crmClass);
			studentService.updateCrmStudent(crmStudent);
			CrmStaff crmStaff=(CrmStaff)request.getSession().getAttribute("crmStaff");
			CrmStation crmStation=new CrmStation(null, crmStudent, flag, crmStaff.getStaffId(), System.currentTimeMillis(), beforeClassId, Long.valueOf(afterClassId));
			studentService.addStation(crmStation);
			//更新升级
			int upgradeCount = studentService.findAdvanceCountbyother(Long.valueOf(afterClassId));
			int totalCount=studentService.findStudentCountbyother(Long.valueOf(afterClassId));
			crmClass.setUpgradeCount(upgradeCount);
			crmClass.setTotalCount(totalCount);
			classservice.updateClass(crmClass);
			//更新学生总数
			CrmClass crmClass2 = classservice.findClassbyId(Long.valueOf(beforeClassId));
			int totalCount2 = studentService.findStudentCountbyother(Long.valueOf(beforeClassId));
			crmClass2.setTotalCount(totalCount2);
			classservice.updateClass(crmClass2);
			mv.setViewName("/findStationByPage.do");
		}
		return mv;
}
	@RequestMapping("/findStationByPage.do")
	public ModelAndView findStationByPage(HttpServletRequest request,HttpServletResponse response){
		 ModelAndView mv=new ModelAndView();
	    	String page_=request.getParameter("page");
	    	int page=1;
	    	int size=3;
	    	if(page_!=null){
	    		page=Integer.parseInt(page_);
	    	}
	    PageStation pageStation = studentService.findStationByPage(page, size);
	    	mv.addObject("pageStation", pageStation);
	    	mv.setViewName("/pages/station/listStation.jsp");
	    	return mv;
}
	@RequestMapping("/checkoneStudent.do")
	public ModelAndView checkoneStudent(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId=request.getParameter("studentId");
		CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
	    mv.addObject("studentDto", studentDto);
	    mv.setViewName("/pages/student/showStudent.jsp");
		return mv;
		
	}
	@RequestMapping("/preaddRunoff.do")
	public ModelAndView preaddRunoff(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId=request.getParameter("studentId");
		CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
		if("退学".equals(studentDto.getStatus())||"毕业".equals(studentDto.getStatus())){
			mv.setViewName("/pages/station/tiaozhuan.jsp");
		}else{
		mv.addObject("studentDto", studentDto);
		mv.setViewName("/pages/runoff/addRunoff.jsp");
		}
		return mv;
}
	@RequestMapping("/addRunoff.do")
	public ModelAndView addRunoff(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String studentId=request.getParameter("studentId");
		CrmStudent crmStudent = studentService.findCrmStudentbyId(Long.valueOf(studentId));
		crmStudent.setStatus("退学");
		studentService.updateCrmStudent(crmStudent);
		String isRefund=request.getParameter("isRefund");
		String refundAmount="";
		if("退款".equals(isRefund)){
		refundAmount=request.getParameter("refundAmount");
		}else{
		refundAmount="0";
		}
		String remark = request.getParameter("remark");
		CrmStaff crmStaff=(CrmStaff)request.getSession().getAttribute("crmStaff");
		CrmRunoff runoff=new CrmRunoff(null, crmStudent,crmStaff.getStaffId(), System.currentTimeMillis(), isRefund, refundAmount, remark);
		studentService.addRunoff(runoff);
		//更新学生总数
		CrmStudentDto studentDto = studentService.findCrmStudentDtobyId(Long.valueOf(studentId));
		Long classId=studentDto.getClassId();
		CrmClass crmClass = classservice.findClassbyId(Long.valueOf(classId));
		int totalCount = studentService.findStudentCountbyother(Long.valueOf(classId));
		int runoffCount=studentService.findRunoffCountbyother(Long.valueOf(classId));
		crmClass.setTotalCount(totalCount);
		crmClass.setRunoffCount(runoffCount);
		classservice.updateClass(crmClass);
		
		mv.setViewName("findRunoffByPage.do");
		return mv;
	
}
	@RequestMapping("/findRunoffByPage.do")
	public ModelAndView findRunoffByPage(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
    	String page_=request.getParameter("page");
    	int page=1;
    	int size=3;
    	if(page_!=null){
    		page=Integer.parseInt(page_);
    	}
     PageRunoff pageRunoff = studentService.findRunoffByPage(page, size);
    	mv.addObject("pageRunoff", pageRunoff);
    	mv.setViewName("/pages/runoff/listRunoff.jsp");
    	return mv;
}
}